import { _decorator, Animation, Component, Input, Label, Node, Sprite, tween, Tween } from 'cc';
import { GunInfo } from '../../../Data/GlobaInterface';
const { ccclass, property } = _decorator;

@ccclass('SinglePao')
/**
 * 单个大炮
 */
export class SinglePao extends Component {

    //大炮纹理
    @property(Sprite)
    private Gun: Sprite = null;

    //大炮数量
    @property(Label)
    private GunNumLabel: Label = null;

    //大炮信息
    public GunInfo: GunInfo = {
        Style: 0,
        Num: 0
    }

    protected start(): void {
        this.node.on(Input.EventType.TOUCH_START, () => {
            this.GunInfo.Num++;
            this.GunNumLabel.string = this.GunInfo.Num.toString();
        }, this)
    }

    /**
     * 更新大炮
     */
    public UpdateGunNum(_num: number) {
        this.GunInfo.Num = _num;
        this.GunNumLabel.string = this.GunInfo.Num.toString();
    }

    /**
     * 更新样式
     */
    public UpdateGunStyle(_gun: number) {
        this.GunInfo.Style = _gun;
        this.Gun.spriteFrame = app.resManager.SpriteFrames["game_singlepao" + (this.GunInfo.Style + 1)];
    }

    /**
     * 添加数量
     */
    public AddGunNum(_guninfo: GunInfo) {
        if (_guninfo.Style != this.GunInfo.Style) return;
        this.GunInfo.Num += _guninfo.Num;
        this.GunNumLabel.string = this.GunInfo.Num.toString();
    }

    /**
     * 发射
     */
    public IamFire() {
        console.log("发射了么", this.GunInfo.Num, this.GunInfo.Style)
        if (this.GunInfo.Num <= 0) return;
        this.Gun.node.getComponent(Animation).play();
        this.GunInfo.Num--;
        this.GunNumLabel.string = this.GunInfo.Num.toString();
    }

    /**
     * 更换角度
     */
    public UpdateAngle(_angle: number) {
        this.Gun.node.angle = _angle * 180 / Math.PI - 90;
        Tween.stopAllByTarget(this.Gun.node);
        tween(this.Gun.node).to(0.3, { angle: 0 }).start();
    }

}


